﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using MethodWorx.CMS.TemplateLanguage.Properties;
using MethodWorx.CMS.TemplateLanguage.Design;
using System.Web.UI.WebControls;
using MethodWorx.CMS.Xml;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Html
{
    public class TextBox : IDesignerInitialization
    {
        public TextBox()
        {
            this.Width = 200;
        }

        [Property(Alias="Name")]
        public string ControlName { get; set; }

        [Property]
        public string Label { get; set; }

        [Property]
        public string Value { get; set; }

        [UnitProperty(Unit="px")]
        public int Width { get; set; }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            var c = new StringBuilder();

            if (!string.IsNullOrEmpty(this.Label))
                c.AppendFormat("<label>{0}</label>", this.Label);

            c.Append("<input type=\"" + this.Type + "\" name=\"" + this.ControlName + "\" value=\"" +
                     (HttpContext.Current.Request[this.ControlName] ?? this.Value) + "\" />");

            return c.ToString();
        }

        protected virtual string Type { get { return "text";  } }

        public virtual string Name
        {
            get { return "textbox"; }
        }

        public string Namespace
        {
            get { return "html"; }
        }

        public string[] Parameters
        {
            get { return new string[] { "name", "value" }; }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("Html:" + this.Name)
                .AddContentBeforeChildren(
                    (this.Label ?? "") + "<span style=\"display:inline-block;width:" + this.Width  + "px;height:15px;padding:2px;background-color:white;border:1px solid silver\">" +
                    this.Value + "</span>");
        }

        public string AddToTemplate(IXmlElement input, string initializer)
        {
            var content = "<html:" + this.Name + " />";
            switch(initializer.ToLower())
            {
                case "insert after":
                    return input.OuterXml + "\r\n" + content;
                    break;
                case "insert before":
                    return content + "\r\n" + input.OuterXml;
                    break;
                case "insert child":
                    input.Append("\r\n" + content);
                    return input.OuterXml;
                    break;
                default:
                    return content;
                    break;
            }
        }

        public string[] GetInitializers()
        {
            return new string[]
                       {
                           "Insert After",
                           "Insert Before",
                           "Insert Child"
                       };
        }
    }
}
